//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.customization.xinri;

import com.api.hrm.util.PageUidFactory;
import com.cloudstore.dev.api.util.Util_TableMap;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.PageIdConst;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.finance.SalaryComInfo;
import weaver.hrm.resource.AllManagers;
import weaver.systeminfo.SystemEnv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class WorkflowFreeNodeAction extends AbstractCommonCommand<Map<String, Object>> {
    public WorkflowFreeNodeAction(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    public BizLogContext getLogContext() {
        return null;
    }

    public Map<String, Object> execute(CommandContext params) {
        HashMap user = new HashMap();
        Boolean hasRight = this.hasRight();
        if (!hasRight) {
            user.put("status", "-1");
            user.put("message", SystemEnv.getHtmlLabelName(2012, this.user.getLanguage()));
            return user;
        } else {
            String deptId = Util.null2String(this.params.get("deptId"));
            String workstatus = Util.null2String(this.params.get("workstatus"));
            String payYearSelect = Util.null2String(this.params.get("payYearSelect"));
            String payYear = Util.null2String(this.params.get("payYear"));
            if (!payYearSelect.equals("") && !payYearSelect.equals("0") && !payYearSelect.equals("6")) {
                payYear = TimeUtil.getDateByOption(payYearSelect, "0").substring(0, 4);
                payYear = TimeUtil.getDateByOption(payYearSelect, "1").substring(0, 4);
            }

            String payYearBeg = payYear + "-01";
            String payYearEnd = payYear + "-12";
            boolean flag = false;
            String params1 = "";
            String params2 = "";
            String params3 = "";
            String params4 = "";
            SalaryComInfo salaryComInfo = new SalaryComInfo();
            String sqlwhere = "";
            if (!payYearBeg.equals("")) {
                sqlwhere = sqlwhere + " and t2.paydate is not null and t2.paydate >='" + payYearBeg + "' ";
            }

            if (!payYearEnd.equals("")) {
                sqlwhere = sqlwhere + " and t2.paydate is not null and t2.paydate <='" + payYearEnd + "' ";
            }

            if (!workstatus.equals("") && !workstatus.equals("9")) {
                if (workstatus.equals("8")) {
                    sqlwhere = sqlwhere + " and t3.status in (0,1,2,3) ";
                } else {
                    sqlwhere = sqlwhere + " and t3.status =" + workstatus + " ";
                }
            }

            if (!deptId.equals("")) {
                sqlwhere = sqlwhere + " and t1.departmentid = " + deptId;
            }

            String selectSql = "SELECT DISTINCT t1.itemid FROM HrmSalaryPaydetail t1, HrmSalaryPay t2,HrmResource t3 WHERE t1.payid = t2.id and t1.hrmid = t3.id " + sqlwhere;
            RecordSet rs = new RecordSet();
            rs.executeQuery(selectSql, new Object[0]);
            int user5 = rs.getCounts();
            params1 = params1 + "<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(124, this.user.getLanguage()) + "\" column=\"departmentid\" orderkey=\"departmentid\" transmethod=\"weaver.hrm.company.DepartmentComInfo.getDepartmentname\"  display=\"true\"  showType=\"0\" isInputCol=\"false\" isPrimarykey=\"false\" />";

            String params9;
            String user0;
            while(rs.next()) {
                params9 = Util.null2String(rs.getString("itemId"));
                user0 = Util.toScreen(salaryComInfo.getSalaryname(params9.indexOf("_") > -1 ? params9.substring(0, params9.indexOf("_")) : params9), this.user.getLanguage());
                params1 = params1 + "<col width=\"10%\" text=\"" + user0 + "\" column=\"itemId" + params9 + "\" />";
                params2 = params2 + ",itemId" + params9;
                params3 = params3 + "|%.2f";
                if (rs.getDBType().equalsIgnoreCase("oracle")) {
                    params4 = params4 + ",'" + params9 + "' itemId" + params9 + "";
                } else if (rs.getDBType().equalsIgnoreCase("mysql")) {
                    params4 = params4 + ",Max(case a.itemid when '" + params9 + "' then IFNULL(allsalary,0) else 0 end ) 'itemId" + params9 + "' ";
                } else {
                    params4 = params4 + ",[itemId" + params9 + "]";
                }
            }

            params2 = params2.length() > 0 ? params2.substring(1) : "";
            params3 = params3.length() > 0 ? params3.substring(1) : "";
            params4 = params4.length() > 0 ? params4.substring(1) : "";
            if (user5 == 0) {
                user.put("hasDatas", false);
                return user;
            } else {
                params9 = " * ";
                user0 = "";
                if (rs.getDBType().equalsIgnoreCase("oracle")) {
                    user0 = " from (select t1.departmentid, t1.itemid, sum(salary) as allsalary  from HrmSalaryPaydetail t1, HrmSalaryPay t2, HrmResource t3 where t1.payid = t2.id and t1.hrmid = t3.id " + sqlwhere + " group by t1.departmentid, t1.itemid order by t1.departmentid)  pivot(sum(allsalary) for itemid in (" + params4 + ") )";
                } else if (rs.getDBType().equalsIgnoreCase("mysql")) {
                    user0 = " from (select a.departmentid," + params4 + " from (select t1.departmentid, t1.itemid, sum(salary) as allsalary  from HrmSalaryPaydetail t1, HrmSalaryPay t2, HrmResource t3 where t1.payid = t2.id and t1.hrmid = t3.id " + sqlwhere + " group by t1.departmentid, t1.itemid) a group by a.departmentid) t";
                } else {
                    user0 = " from (select t1.departmentid, 'itemId'+t1.itemid itemId, sum(salary) as allsalary  from HrmSalaryPaydetail t1, HrmSalaryPay t2, HrmResource t3 where t1.payid = t2.id and t1.hrmid = t3.id " + sqlwhere + " group by t1.departmentid, t1.itemid ) AS C  pivot(sum(allsalary) for itemid in (" + params4 + ") ) AS T";
                }

                String user1 = "";
                user1 = "<operates width=\"10%\">";
                user1 = user1 + " <popedom transmethod=\"weaver.hrm.common.SplitPageTagOperate.getBasicOperate\" otherpara=\"true\"></popedom> ";
                user1 = user1 + "     <operate href=\"javascript:openDialog();\" text=\"" + SystemEnv.getHtmlLabelName(22045, this.user.getLanguage()) + "\" index=\"0\" />";
                user1 = user1 + "</operates>";
                String user2 = PageUidFactory.getHrmPageUid("DeptSalaryRpList");
                String user3 = "";
                user3 = " <table pageUid=\"" + user2 + "\" pageId=\"" + "Hrm:DeptSalaryRpList" + "\" pagesize=\"" + PageIdConst.getPageSize("Hrm:DeptSalaryRpList", this.user.getUID(), "Hrm") + "\" tabletype=\"none\"> <sql backfields=\"" + params9 + "\" sqlform=\"" + Util.toHtmlForSplitPage(user0) + "\" sqlwhere=\"\"  sqlorderby=\"\"  sqlprimarykey=\"departmentid\" sqlsortway=\"Asc\" sqlisdistinct=\"false\" sumColumns=\"" + params2 + "\" decimalFormat=\"" + params3 + "\" />" + user1 + "\t<head>" + params1 + "\t</head> </table>";
                String user4 = user2 + "_" + Util.getEncrypt(Util.getRandom());
                Util_TableMap.setVal(user4, user3);
                user.put("sessionkey", user4);
                user.put("colNum", user5 + 1);
                return user;
            }
        }
    }

    private Boolean hasRight() {
        boolean params = false;

        try {
            String user;
            if (this.user.isAdmin()) {
                user = "select * from HrmRoleMembers where roleid=4 and resourceid=" + this.user.getUID();
                RecordSet hasRight = new RecordSet();
                hasRight.executeQuery(user, new Object[0]);
                if (hasRight.next()) {
                    params = true;
                }
            } else {
                user = "" + this.user.getUID();
                ArrayList payYearEnd = new ArrayList();
                AllManagers deptId = new AllManagers();
                String workstatus = " SELECT resourceid FROM (  SELECT a.id AS resourceid, b.roleid , b.rolelevel FROM HrmResource a, HrmRoleMembers b  WHERE (a.id=b.resourceid and b.resourcetype=1)  UNION ALL  SELECT a.id AS resourceid, b.roleid , b.rolelevel FROM HrmResourceManager a, HrmRoleMembers b  WHERE (a.id=b.resourceid and b.resourcetype IN(7,8))  UNION ALL  SELECT a.id AS resourceid, b.roleid , b.rolelevel FROM HrmResource a, HrmRoleMembers b  WHERE (a.subcompanyid1 = b.resourceid AND a.seclevel>=b.seclevelfrom AND a.seclevel<=b.seclevelto AND b.resourcetype=2)  UNION ALL  SELECT a.id AS resourceid, b.roleid , b.rolelevel FROM HrmResource a, HrmRoleMembers b  WHERE (a.departmentid = b.resourceid AND a.seclevel>=b.seclevelfrom AND a.seclevel<=b.seclevelto AND b.resourcetype=3)  UNION ALL  SELECT a.id AS resourceid, b.roleid , b.rolelevel FROM HrmResource a, HrmRoleMembers b  WHERE  (a.jobtitle = b.resourceid AND b.resourcetype=5 AND (b.jobtitlelevel=1 OR (b.jobtitlelevel=2 AND a.subcompanyid1 IN(b.subdepid)) OR (b.jobtitlelevel=3 AND a.departmentid IN(b.subdepid))))) t  WHERE resourceid >1 and roleid=4";
                RecordSet payYearSelect = new RecordSet();
                payYearSelect.executeQuery(workstatus, new Object[0]);

                while(payYearSelect.next()) {
                    String payYear = payYearSelect.getString("resourceid");
                    payYearEnd.add(payYear);
                    deptId.getAll(payYear);

                    while(deptId.next()) {
                        payYearEnd.add(deptId.getManagerID());
                    }
                }

                for(int flag = 0; flag < payYearEnd.size(); ++flag) {
                    if (user.equals((String)payYearEnd.get(flag))) {
                        params = true;
                    }
                }
            }
        } catch (Exception payYearBeg) {
            payYearBeg.printStackTrace();
        }

        return params;
    }
}
